package com.sltx.service.impl;


import com.sltx.dao.aaa.QyyjMapper;
import com.sltx.service.Qyyjservice;
import com.sltx.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class QyyjServiceImpl implements Qyyjservice {
    @Autowired
    private QyyjMapper qyyjMapper;


    /*获取红色预警企业*/
    @Override
    public List<Object> getredAll(PageData pd) {
        List<Object> result = new  ArrayList<>();
        //获取上一个月的红色预警企业个数
        Map map = qyyjMapper.getredNum(pd);
        //获取上一个月的红色预警企业数据
        List<Map> maps = qyyjMapper.getredAll(pd);
        if (maps != null && !maps.isEmpty()){
            result.add(map);
            result.add(maps);
        }else {
            //获取上两个月的红色预警企业个数
        Map map2 = qyyjMapper.getredNum2(pd);
        //获取上两个月的红色预警企业数据
        List<Map> maps2 = qyyjMapper.getredAll2(pd);
            result.add(map2);
            result.add(maps2);
        }
        return result;
    }

    //获取橙色预警企业
    @Override
    public List<Object> getorangeAll(PageData pd) {
        List<Object> result = new  ArrayList<>();
        //获取上一个月的橙色预警企业个数
        Map map = qyyjMapper.getorangeNum(pd);
        //获取上一个月的橙色预警企业数据
        List<Map> maps = qyyjMapper.getorangeAll(pd);
        if (maps != null && !maps.isEmpty()){
            result.add(map);
            result.add(maps);
        }else {
            //获取上两个月的橙色预警企业个数
            Map map2 = qyyjMapper.getorangeNum2(pd);
            //获取上两个月的橙色预警企业数据
            List<Map> maps2 = qyyjMapper.getorangeAll2(pd);
            result.add(map2);
            result.add(maps2);
        }

        return result;
    }

    //获取黄色预警企业
    @Override
    public List<Object> getyellowAll(PageData pd) {
        List<Object> result = new  ArrayList<>();
        Map map = qyyjMapper.getyellowNum(pd);
        List<Map> maps = qyyjMapper.getyellowAll(pd);
        if (maps != null && !maps.isEmpty()){
            result.add(map);
            result.add(maps);
        }else {
            //获取上两个月的预警企业个数
            Map map2 = qyyjMapper.getyellowNum2(pd);
            //获取上两个月的预警企业数据
            List<Map> maps2 = qyyjMapper.getyellowAll2(pd);
            result.add(map2);
            result.add(maps2);
        }
        return result;
    }

    @Override
    public List<? extends Object> getweigui(String entName) {
        List<String> list = new ArrayList<>();
        List<Map> red = qyyjMapper.getweiguiRed(entName);
        List<Map> orange = qyyjMapper.getweiguiOrange(entName);
        List<Map> yellow = qyyjMapper.getweiguiYellow(entName);
        if (red.size()>0){
            return red;
        }else  if (orange.size()>0){
            return orange;
        }else if (yellow.size()>0) {
            return yellow;
        }else {
            list.add("暂无");
            return list;

        }

    }

    //获取红色预警最近年月
    @Override
    public String bigQuarter() {
        return qyyjMapper.bigQuarter();
    }



    //预警企业筛选条件查询
    @Override
    public Map<String, Object> getCondition(PageData pd) {
        HashMap<String, Object> map = new HashMap<>();
        //1.先判断此用户是否存在
        String user = qyyjMapper.getUser(pd);
        //如果为空
        if (user == null){
        qyyjMapper.insterTihiaojian(pd);
        }
        //1.根据用户名称查询红色预警项 3
       List<Object> Red =  qyyjMapper.getRedyujing(pd);
       //根据用户名查询橙色预警项 2
        List<Object> Orange = qyyjMapper.getOrangeyujing(pd);
        //根据用户名查询黄色预警项 1
        List<Object> Yellow = qyyjMapper.getYellowyujing(pd);
        //查询筛选条件
        List<Object> Moren = qyyjMapper.getMoren(pd);
        map.put("Red",Red);
        map.put("Orange",Orange);
        map.put("Yellow",Yellow);
        map.put("Moren",Moren);
        return map;
    }


    //获取预警企业表单
    @Override
    public Map<String, Object> getList(PageData pd) {
        HashMap<String, Object> map = new HashMap<>();
            //获取红色预警企业 查询上一个月的
            List<Map<String,Object>> Red = qyyjMapper.getRed(pd);
            //获取橙色预警企业 查询上一个月的
            List<Map<String,Object>> Orange = qyyjMapper.getOrange(pd);
            //获取黄色预警企业 查询上一个月的
            List<Map<String,Object>> Yellow = qyyjMapper.getYellow(pd);
            if (Red.size() >0 || Yellow.size()>0 || Orange.size()>0){
                Integer Rednum = qyyjMapper.getRednum(pd);
                map.put("Red",Red);
                map.put("Rednum",Rednum);
            }else {
                //查询第二个月的
                List<Map<String,Object>> Red2 = qyyjMapper.getRed2(pd);
                Integer Rednum = qyyjMapper.getRednum2(pd);
                map.put("Red",Red2);
                map.put("Rednum",Rednum);
            }
            if (Orange.size()>0 || Red.size()>0 || Yellow.size()>0){
                Integer Orangenum = qyyjMapper.getOrangenum(pd);
                map.put("Orange",Orange);
                map.put("Orangenum",Orangenum);
            }else {
                //查询第二个月的
                List<Map<String,Object>> Orange2 = qyyjMapper.getOrange2(pd);
                Integer Orangenum = qyyjMapper.getOrangenum2(pd);
                map.put("Orange",Orange2);
                map.put("Orangenum",Orangenum);
            }
            if (Yellow.size()>0 || Orange.size()>0 || Red.size()>0 ){
                Integer Yellownum = qyyjMapper.getYellownum(pd);
                map.put("Yellow",Yellow);
                map.put("Yellownum",Yellownum);
            }else {
                //查询第二个月的
                List<Map<String,Object>> Yellow2 = qyyjMapper.getYellow2(pd);
                Integer Yellownum = qyyjMapper.getYellownum2(pd);
                map.put("Yellow",Yellow2);
                map.put("Yellownum",Yellownum);
            }
        Map<String, Object> map1 = new HashMap<>();

        if(pd.get("date") != null){
            String date = (String) pd.get("date");
            map1.put("update_date", date);
               map.put("update_date",map1);
            }else {
                Map<String,Object> year = qyyjMapper.getYear();
                map.put("update_date",year);
            }
        return map;
    }

    //删除条件
    @Override
    public void delect(PageData pd) {
        //根据用户id，变更项删除此筛选条件
        qyyjMapper.delectTiaojian(pd);
        // todo 根据删除筛选条件添加到默认筛选条件表
        //yyjMapper.inster1(pd);
    }

    //用户筛选条件添加
    @Override
    public void inster(PageData pd) {
        //添加用户id，变更项，条件，变更等级
        qyyjMapper.insterTiaojian(pd);
        // todo 根据删除筛选条件删除默认筛选条件表
        //qyyjMapper.delect1(pd);
    }

    //查询预警表单数据(普通用户)
    @Override
    public Map<String, Object> getYujing(PageData pd) {
        HashMap<String, Object> map = new HashMap<>();
        //获取红色预警企业 查询当月的
        List<Map<String,Object>> Red = qyyjMapper.getRed3(pd);
        //获取橙色预警企业 查询当月的
        List<Map<String,Object>> Orange = qyyjMapper.getOrange3(pd);
        //获取黄色预警企业 查询当月的
        List<Map<String,Object>> Yellow = qyyjMapper.getYellow3(pd);
        if (Red.size() >0 || Yellow.size()>0 || Orange.size()>0){
            Integer Rednum = qyyjMapper.getRednum3(pd);
            map.put("Red",Red);
            map.put("Rednum",Rednum);
        }else {
            //查询上一个月的
            List<Map<String,Object>> Red2 = qyyjMapper.getRed4(pd);
            Integer Rednum = qyyjMapper.getRednum4(pd);
            map.put("Red",Red2);
            map.put("Rednum",Rednum);
        }
        if (Orange.size()>0 || Red.size()>0 || Yellow.size()>0){
            Integer Orangenum = qyyjMapper.getOrangenum3(pd);
            map.put("Orange",Orange);
            map.put("Orangenum",Orangenum);
        }else {
            //查询上一个月的
            List<Map<String,Object>> Orange2 = qyyjMapper.getOrange4(pd);
            Integer Orangenum = qyyjMapper.getOrangenum4(pd);
            map.put("Orange",Orange2);
            map.put("Orangenum",Orangenum);
        }
        if (Yellow.size()>0 || Orange.size()>0 || Red.size()>0 ){
            Integer Yellownum = qyyjMapper.getYellownum3(pd);
            map.put("Yellow",Yellow);
            map.put("Yellownum",Yellownum);
        }else {
            //查询上一个月的
            List<Map<String,Object>> Yellow2 = qyyjMapper.getYellow4(pd);
            Integer Yellownum = qyyjMapper.getYellownum4(pd);
            map.put("Yellow",Yellow2);
            map.put("Yellownum",Yellownum);
        }
        Map<String, Object> map1 = new HashMap<>();

        if(pd.get("date") != null){
            String date = (String) pd.get("date");
            map1.put("update_date", date);
            map.put("update_date",map1);
        }else {
            Map<String,Object> year = qyyjMapper.getYear3();
            map.put("update_date",year);
        }
        return map;
    }



    @Override
    public void upOrange(PageData pd) {
        //升级为橙色预警
        qyyjMapper.upOrange(pd);
    }

    @Override
    public void upRed(PageData pd) {
        //升级为红色预警
        qyyjMapper.upRed(pd);
    }

    @Override
    public void upOrange2(PageData pd) {
        //降级为橙色预警
        qyyjMapper.upOrange2(pd);
    }

    @Override
    public void upYellow2(PageData pd) {
        //降级为黄色预警
        qyyjMapper.upYellow2(pd);
    }


}



